/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import java.util.Collection;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import models.Comments;
import models.Messages;
import models.Questions;
import models.Users;
import services.Impl.CommentsServiceImpl;
import services.Impl.MessageServiceImpl;
import services.Impl.QuestionsServiceImpl;

/**
 *
 * @author amega
 */
@ManagedBean(name="faqBean")
@SessionScoped
public class FaqBean {
    
    @EJB
    private QuestionsServiceImpl questionsService;
    @EJB
    private CommentsServiceImpl commentsService;
    @EJB
    private MessageServiceImpl messageService;
    
    public Collection<Questions> getAllQuestions(){
        return questionsService.findAll();
    }
    
    public FaqBean(){}
    
    private Users currentUser;
    private Questions currentQ;
    private String mess;
    
    public void attList(ActionEvent event) {
        this.currentUser = (Users)event.getComponent().getAttributes().get("user");
        this.currentQ = (Questions)event.getComponent().getAttributes().get("curQ");
    }
    
    public String addMessage() {
        Questions message = new Questions();
        message.setIdUser(currentUser);
        message.setText(this.mess);
        questionsService.create(message);
        this.mess = null;
        return "";
    }

    public String getMess() {
        return mess;
    }

    public void setMess(String mess) {
        this.mess = mess;
    }
    
    public String addQuestionAnswer() {
        Comments com = new Comments();
        com.setDatePublished(new Date());
        com.setIdUser(currentUser.getId().intValue());
        com.setText(mess);
        com.setIdQuestion(currentQ);
        Users autor = currentQ.getIdUser();
        Messages message = new Messages();
        message.setDateTime(new Date());
        message.setIdUserTo(autor);
        message.setIdUserFrom(currentUser);
        message.setIsRead(Boolean.FALSE);
        message.setText("You hane new answer: "+currentQ.getText()+"\nAnswer: "+this.mess);
        messageService.create(message);
        commentsService.create(com);
        this.mess = null;
        return "";
    }
    
}
